package com.cheng.im.service.friendship.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheng.im.service.common.resp.CommonResult;
import com.cheng.im.service.friendship.mapper.ImFriendshipGroupMemberMapper;
import com.cheng.im.service.friendship.service.ImFriendshipGroupMemberService;
import com.cheng.im.service.friendship.service.ImFriendshipGroupService;
import com.cheng.im.service.frienship.domain.ImFriendshipGroup;
import com.cheng.im.service.frienship.domain.ImFriendshipGroupMember;
import com.cheng.im.service.frienship.model.req.AddFriendShipGroupMemberReq;
import com.cheng.im.service.frienship.model.req.DeleteFriendShipGroupMemberReq;
import com.cheng.im.service.user.domain.ImUserData;
import com.cheng.im.service.user.service.ImUserDataService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author Cheng
* @description 针对表【im_friendship_group_member】的数据库操作Service实现
* @createDate 2024-12-17 15:04:33
*/
@DubboService
public class ImFriendshipGroupMemberServiceImpl extends ServiceImpl<ImFriendshipGroupMemberMapper, ImFriendshipGroupMember>
    implements ImFriendshipGroupMemberService {

    @Resource
    private ImFriendshipGroupService imFriendshipGroupService;
    @Resource
    private ImUserDataService imUserDataService;
    @Resource
    private ImFriendshipGroupMemberService thisService;

    @Override
    @Transactional
    public CommonResult addGroupMember(AddFriendShipGroupMemberReq req) {
        CommonResult<ImFriendshipGroup> group = imFriendshipGroupService.getGroup(req.getFromId(),req.getGroupName(),req.getAppId());
        if(!group.isOk()){
            return group;
        }

        List<String> successId = new ArrayList<>();
        for (String toId : req.getToIds()) {
            CommonResult<ImUserData> singleUserInfo = imUserDataService.getSingleUserInfo(toId, req.getAppId());
            if(singleUserInfo.isOk()){
                int i = thisService.doAddGroupMember(group.getData().getGroupId(), toId);
                if(i == 1){
                    successId.add(toId);
                }
            }
        }

        Long seq = imFriendshipGroupService.updateSeq(req.getFromId(), req.getGroupName(), req.getAppId());
        // todo: im
//        AddFriendGroupMemberPack pack = new AddFriendGroupMemberPack();
//        pack.setFromId(req.getFromId());
//        pack.setGroupName(req.getGroupName());
//        pack.setToIds(successId);
//        pack.setSequence(seq);
//        messageProducer.sendToUserExceptClient(req.getFromId(), FriendshipEventCommand.FRIEND_GROUP_MEMBER_ADD,
//                pack,new ClientInfo(req.getAppId(),req.getClientType(),req.getImei()));

        return CommonResult.success(successId);
    }

    @Override
    public CommonResult delGroupMember(DeleteFriendShipGroupMemberReq req) {
        CommonResult<ImFriendshipGroup> group = imFriendshipGroupService.getGroup(req.getFromId(),req.getGroupName(),req.getAppId());
        if(!group.isOk()){
            return group;
        }

        List<String> successId = new ArrayList<>();
        for (String toId : req.getToIds()) {
            CommonResult<ImUserData> singleUserInfo = imUserDataService.getSingleUserInfo(toId, req.getAppId());
            if(singleUserInfo.isOk()){
                int i = deleteGroupMember(group.getData().getGroupId(), toId);
                if(i == 1){
                    successId.add(toId);
                }
            }
        }

        Long seq = imFriendshipGroupService.updateSeq(req.getFromId(), req.getGroupName(), req.getAppId());

        // todo: im
//        DeleteFriendGroupMemberPack pack = new DeleteFriendGroupMemberPack();
//        pack.setFromId(req.getFromId());
//        pack.setGroupName(req.getGroupName());
//        pack.setToIds(successId);
//        pack.setSequence(seq);
//        messageProducer.sendToUserExceptClient(req.getFromId(), FriendshipEventCommand.FRIEND_GROUP_MEMBER_DELETE,
//                pack,new ClientInfo(req.getAppId(),req.getClientType(),req.getImei()));
        return CommonResult.success(successId);
    }

    @Override
    public int doAddGroupMember(Long groupId, String toId) {
        ImFriendshipGroupMember imFriendShipGroupMemberEntity = new ImFriendshipGroupMember();
        imFriendShipGroupMemberEntity.setGroupId(groupId);
        imFriendShipGroupMemberEntity.setToId(toId);

        try {
            int insert = baseMapper.insert(imFriendShipGroupMemberEntity);
            return insert;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int clearGroupMember(Long groupId) {
        LambdaQueryWrapper<ImFriendshipGroupMember> query = new LambdaQueryWrapper<>();
        query.eq(ImFriendshipGroupMember::getGroupId,groupId);
        int delete = baseMapper.delete(query);
        return delete;
    }

    public int deleteGroupMember(Long groupId, String toId) {
        LambdaQueryWrapper<ImFriendshipGroupMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImFriendshipGroupMember::getGroupId,groupId);
        queryWrapper.eq(ImFriendshipGroupMember::getToId,toId);

        try {
            int delete = baseMapper.delete(queryWrapper);
//            int insert = imFriendShipGroupMemberMapper.insert(imFriendShipGroupMemberEntity);
            return delete;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}




